<?php

use \Orm\BaseModels;
use Helper\StringHelper;

class StoreModel extends BaseModels
{
    private $defaultField = 'id, logoImg, logoImg as logo_img_old, storesName,createTime,mainBuiness,province_id,city_id,address, detail,userId,mobilephone,company_name,shop_type,visits,faxCode,zipCode,email,businessImg,businessImg as businessImg_old,idcardFrontImg,idcardFrontImg as idcardFrontImg_old,idcardBackImg, idcardBackImg as idcardBackImg_old, store_status,storesName as stores_name_conver, company_name as company_name_convert ,details';
    // 设置当前模型的数据库连接
//    protected $connection = ['database' => 'mtytest'];

    /**
     * 根据id获取单条记录
     * @param $id
     * @param string $field
     * @return array|null|PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneById($id, $field = '')
    {
        $field = empty($field) ? $this->defaultField : $field;
        $where = ['id'=>$id,'is_logoff'=>0];
        $dataOne = $this->field($field)->where($where)->find();

        return empty($dataOne) ? $dataOne : $dataOne->toArray();
    }


    /**
     * 自定义条件获取单条记录
     * @param $where
     * @param string $field
     * @return array|null|PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneData($where, $field = '')
    {
        $field = empty($field) ? $this->defaultField : $field;
        $dataOne = $this->where($where)->field($field)->find();
        return empty($dataOne) ? $dataOne : $dataOne->toArray();
    }

    /**
     * 统计当前开店人数
     * @return int|string
     */
    public function countStores(){
        return $this->where(['store_status'=>'PASSED','is_logoff'=>0])->count();
    }

    /**
     * 统计店铺访问人数
     * @param string $userid
     * @return mixed
     */
    public function countVisit($userid=''){
        return $this->where(['userid'=>$userid,'store_status'=>'PASSED'])->value('visits');
    }

    /**
     * 推荐商家列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recommendStore(){
        $field = "t1.id,t1.logoImg,t1.storesName,t1.mainBuiness,t1.storesName as stores_name_convert,t1.mainBuiness as main_buiness_short,shop_type";
        $result = $this->Alias('t1')
            ->field($field)
            ->order('t1.recommendTime DESC, t1.id desc')
            ->where(['t1.store_status' => 'PASSED','t1.is_logoff'=>0])
            ->where('t1.recommendTime > 0')
            ->order('t1.recommendTime desc')
            ->limit(2)
            ->select()->toArray();
        if (empty($result)){
            $result = [];
        }
        return $result;
    }

    /************ 获取器 ************/

    //脱敏处理后的店铺名称
    public function getStoresNameConvertAttr($value){
        return StringHelper::convertZhStr($value);
    }
    public function getCompanyNameConvertAttr($value){
        return StringHelper::convertZhStr($value);
    }

    //简短的主营产品
    public function getmainBuinessShortAttr($value){
        $length = 15;
        if (mb_strlen($value) > $length){
            $value = StringHelper::subString_UTF8($value,0,15) . '...';
        }
        return $value;
    }
    //图片URL
    public function getLogoImgAttr($value){
        return empty($value) ? '' : IMG_URL . $value;
    }
    //图片URL
    public function getBusinessImgAttr($value){
        return empty($value) ? '' : IMG_URL . $value;
    }
    //图片URL
    public function getIdcardFrontImgAttr($value){
        return empty($value) ? '' : IMG_URL . $value;
    }
    //图片URL
    public function getIdcardBackImgAttr($value){
        return empty($value) ? '' : IMG_URL . $value;
    }
    //开店日期
    public function getCreateDateAttr($value){
        return empty($value) ? '' : date('Y-m-d', $value);
    }

}
